package com.utaustin.mirankerlab.ultrawrap.compiler.main;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import com.utaustin.mirankerlab.ultrawrap.compiler.parser.ParseException;
import com.utaustin.mirankerlab.ultrawrap.compiler.parser.Parser;
import com.utaustin.mirankerlab.ultrawrap.compiler.parser.syntaxtree.Query;

public class RunTest {
	//ok
	static String query = "SELECT ?s ?name " +
	"WHERE{" +
	"?s rdf:type kingdoms ." +
	"?s kingdoms_kingdom_name ?name ." +
	"}"; 
	
	//ok
	static String query1 = "Select ?dataset " +
			"where{ " +
			"protein2520 is_dataset_protein ?x ." +
			"?x dataset_name ?dataset" +
			"}";
	
	//ok
	static String query2 = "SELECT ?protein ?name " +
			"Where { " +
			"dataset16 dataset_protein ?protein . " +
			"dataset17 dataset_protein ?protein . " +
			"dataset18 dataset_protein ?protein . " +
			"?protein protein_name ?name	 " +
			"}";
	//ok
	static String query3 = "SELECT ?protein ?name " +
			"Where { " +
			"dataset18 dataset_protein ?protein .  " +
			"?protein protein_name ?name	 " +
			"}";
	
	//ERROR extra comma before FROM
	static String bsbm1 = "SELECT DISTINCT ?product ?label " +
			"WHERE{ " +
			"?product product_label ?label . " +
			"ProductType47 producttype_product ?product . " +
			"?product product_productfeature ProductFeature76 . " +
			"?product product_productfeature ProductFeature4242 . " +
			"?product product_propertyNum1 ?value . " +
			"FILTER(?value > 500) " +
			"} " +
			"ORDER BY ?label";
	
	//ok
	static String bsbm2 = "SELECT  ?label ?comment ?producer ?productFeature ?propertyTextual1 ?propertyTextual2 ?propertyTextual3 ?propertyNumeric1 ?propertyNumeric2 ?propertyTextual4 ?propertyTextual5 ?propertyNumeric4 " +
			"WHERE{  " +
			"Product43	product_label	?label. " +
			"Product43   product_comment	?comment . " +
			"Product43   product_producer ?p . " +
			"?p          producer_label    ?producer . " +
			"Product43   product_productfeature   ?f . " +
			"?f        	productfeature_label      ?productFeature . " +
			"Product43	product_propertyTex1 ?propertyTextual1 . " +
			"Product43	product_propertyTex2 ?propertyTextual2 . " +
			"Product43	product_propertyTex3 ?propertyTextual3 . " +
			"Product43	product_propertyNum1 ?propertyNumeric1 . " +
			"Product43	product_propertyNum2 ?propertyNumeric2 . " +
			"OPTIONAL{ Product43 product_propertyTex4 ?propertyTextual4 } " +
			"OPTIONAL{ Product43 product_propertyTex5 ?propertyTextual5 }  " +
			"OPTIONAL{ Product43 product_propertyNum4 ?propertyNumeric4 }" +
			"}";
	
	//ok
	static String bsbm2_a = "SELECT  ?label ?comment  " +
	"WHERE{  " +
	"Product43	product_label	?label. " +
	"Product43   product_comment	?comment  " +
	"}";
	
	static String oneTripleA = "SELECT ?x " +
	"WHERE { " +
	"protein2520 is_dataset_protein ?x ." +
	"}";
	
	static String oneTripleB = "SELECT  ?x  " +
	"WHERE {  " +
	"Product43	product_label	?x. " +
	"}";
	
	//error
	
	static String oneTripleC = "SELECT  ?p ?o  " +
	"WHERE {  " +
	"protein2520	?p	?o. " +
	"}";
	
	// ERROR extra , before FROM
	static String bsbm3 = "SELECT ?product ?label  " +
			"WHERE { " +
			"?product product_label ?label . " +
			"ProductType11 producttype_product ?product. " +
			"?product product_productfeature ProductFeature1895 ." +
			"?product product_propertyNum1 ?p1 . " +
			"FILTER ( ?p1 > 50 )  " +
			"?product product_propertyNum3 ?p3 . " +
			"FILTER (?p3 < 300 ) " +
			"OPTIONAL {  " +
			"?product product_productfeature ProductFeature21 . " +
			"?product product_label ?testVar " +
			"} " +
			"FILTER (!bound(?testVar)) " +
			"} " +
			"ORDER BY ?label";
	
	//UNION IS NOT SUPPORTER
	static String bsbm4 = "SELECT DISTINCT ?product ?label ?propertyTextual " +
			"WHERE { " +
			"{  " +
			"?product product_label ?label . " +
			"ProductType47 producttype_product ?product. " +
			"?product product_productfeature ProductFeature75 . " +
			"?product product_productfeature ProductFeature4200 . " +
			"?product product_propertyTex1 ?propertyTextual . " +
			"?product product_propertyNum1 ?p1 . " +
			"FILTER ( ?p1 > 100 ) " +
			"}  " +
			"UNION { " +
			"?product product_label ?label . " +
			"ProductType11 producttype_product ?product. " +
			"?product product_productfeature ProductFeature1895 . " +
			"?product product_productfeature ProductFeature21 . " +
			"?product product_propertyTex1 ?propertyTextual . " +
			"?product product_propertyNum1 ?p2 . " +
			"FILTER ( ?p2> 200 )  " +
			"} " +
			"} " +
			"ORDER BY ?label";
	
	//ERROR
	static String bsbm5 = "SELECT DISTINCT  ?product ?productLabel " +
			"WHERE{  " +
			"?product  product_label  ?productLabel . " +
			"FILTER ( Product863 != ?product ) " +
			"Product863 product_productfeature   ?prodFeature ." +
			"?product  product_productfeature   ?prodFeature . " +
			"Product863 product_propertyNum1  ?origProperty1 . " +
			"?product  product_propertyNum1  ?simProperty1 . " +
			"FILTER ( ?simProperty1 < ( ?origProperty1 + 120 ) ) " +
			"Product863 product_propertyNum2  ?origProperty2 . " +
			"?product  product_propertyNum2  ?simProperty2 . " +
			"FILTER ( ?simProperty2 < ( ?origProperty2 + 170 ) ) " +
			"} " +
			"ORDER BY ?productLabel";
	
	//ERROR
	static String bsbm6 = "SELECT ?product ?label " +
			"WHERE { " +
			"?product product_label ?label . " +
			"?product rdf:type Product . " +
			"FILTER regex(?label, \"tumble\") " +
					"}";
	
	// ERROR extra , before FROM
	static String bsbm7 = "SELECT ?productLabel ?offer ?price ?vendor ?vendorTitle ?review ?revTitle ?reviewer ?revName ?rating1 ?rating2 " +
			"WHERE { " +
			"Product981 product_label ?productLabel . " +
			"OPTIONAL { " +
			"?offer offer_product Product981 . " +
			"?offer offer_price ?price . " +
			"?offer offer_vendor ?vendor . " +
			"?vendor vendor_label ?vendorTitle . " +
			"?vendor vendor_country \"DE\" . " +
			"?offer offer_vendor ?vendor .  " +
			"?offer offer_validTo ?date . " +
			" } " +
			"OPTIONAL { " +
			"?review review_product Product981 . " +
			"?review review_person ?reviewer . " +
			"?reviewer person_name ?revName . " +
			"?review review_title ?revTitle . " +
			"OPTIONAL { ?review review_rating1 ?rating1 . } " +
			"OPTIONAL { ?review review_rating2 ?rating2 . }  " +
			"}" +
			"}";
	
	// ERROR: starts with (SELECT....
	static String bsbm8 = "SELECT  ?title ?reviewDate ?reviewer ?reviewerName ?rating1 ?rating2 ?rating3 ?rating4 " +
			"WHERE{  " +
			"?review  review_product  Product806 . " +
			"?review  review_title        ?title . " +
			"?review  review_text        ?text . " +
			"?review   review_reviewDate  ?reviewDate . " +
			"?review   review_person      ?reviewer . " +
			"?reviewer  person_name       ?reviewerName . " +
			"OPTIONAL " +
			"{ ?review  review_rating1  ?rating1 .} " +
			"OPTIONAL " +
			"{ ?review  review_rating2  ?rating2 .} " +
			"OPTIONAL " +
			"{ ?review  review_rating3  ?rating3 .} " +
			"OPTIONAL " +
			"{ ?review  review_rating4  ?rating4 .}" +
			" } " +
			"ORDER BY ?reviewerName";
	
	//not supported
	static String bsbm9 = "DESCRIBE ?x " +
			"WHERE { Review4893 review_person ?x }";
	
	//error
	static String bsbm10 = "SELECT DISTINCT ?offer ?price ?date	 " +
			"WHERE { " +
			"?offer offer_product Product981. " +
			"?offer offer_vendor ?vendor . " +
			"?vendor vendor_country \"US\" . " +
			"?offer offer_deliveryDays ?deliveryDays . " +
			"FILTER (?deliveryDays <= 3) " +
			"?offer offer_price ?price . " +
			"?offer offer_validTo ?date . " +
			"} " +
			"ORDER BY ?price";
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 
		try {
			
			System.out.println("---------------------------- query");
			System.out.println(query);
			InputStream is1 = new ByteArrayInputStream(query.getBytes("UTF-8"));
			test(is1);
			System.out.println("---------------------------- query1");
			System.out.println(query1);
			InputStream is2 = new ByteArrayInputStream(query1.getBytes("UTF-8"));
			test(is2);
			System.out.println("---------------------------- query2");
			System.out.println(query2);
			InputStream is3 = new ByteArrayInputStream(query2.getBytes("UTF-8"));
			test(is3);
			System.out.println("---------------------------- query3");
			System.out.println(query3);
			InputStream is4 = new ByteArrayInputStream(query3.getBytes("UTF-8"));
			test(is4);
			
			System.out.println("---------------------------- bsbm2");
			System.out.println(bsbm2);
			InputStream is6 = new ByteArrayInputStream(bsbm2.getBytes("UTF-8"));
			test(is6);
			System.out.println("---------------------------- bsbm2_a");
			System.out.println(bsbm2_a);
			InputStream is7 = new ByteArrayInputStream(bsbm2_a.getBytes("UTF-8"));
			test(is7);
			System.out.println("---------------------------- oneTripleA");
			System.out.println(oneTripleA);
			InputStream is8 = new ByteArrayInputStream(oneTripleA.getBytes("UTF-8"));
			test(is8);
			System.out.println("---------------------------- oneTripleB");
			System.out.println(oneTripleB);
			InputStream is9 = new ByteArrayInputStream(oneTripleB.getBytes("UTF-8"));
			test(is9);
			
			System.out.println("---------------------------- bsbm1");
			System.out.println(bsbm1);
			InputStream is5 = new ByteArrayInputStream(bsbm1.getBytes("UTF-8"));
			test(is5);
			System.out.println("---------------------------- oneTripleC");
			System.out.println(oneTripleC);
			InputStream is10 = new ByteArrayInputStream(oneTripleC.getBytes("UTF-8"));
			test(is10);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static void test(InputStream is) throws ParseException{
		Parser parser = new Parser(is);//feed parser the input stream data
		Query query = parser.Query();//return root node of syntax tree
		//create visitor that is used to traverse tree starting at root 
		SparqlToSql sparql2sql = new SparqlToSql(false);
		//walk the tree with the visitor,sparqlToSql
		query.accept(sparql2sql);
		System.out.println(sparql2sql.getSqlQuery());
		System.out.println("Select Tokens: "+sparql2sql.getSelectTokens());
	}

}
